package com.example.sqlitetest.Activity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sqlitetest.Adapter.LiuYanNumAdapter;
import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;
import com.example.sqlitetest.model.LiuYanNumInfo;


import java.util.ArrayList;
import java.util.List;

public class LiuYanNumListActivity extends AppCompatActivity {
    //定义数据
    private List<LiuYanNumInfo> mData;
    private MyDataBaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numberlist);



        //操作list列表
        final ListView listView = (ListView) findViewById(R.id.list);
        LayoutInflater inflater = getLayoutInflater();

        //初始化数据库对象
        helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对象


        //实例化ListAdapter
        LiuYanNumAdapter adapter = new LiuYanNumAdapter(this, inflater, mData);
        listView.setAdapter(adapter);

        String username =getIntent().getExtras().getString("username");
        Toast.makeText(LiuYanNumListActivity.this,username, Toast.LENGTH_SHORT).show();
//        geLiuYanInfoList(username);

    }
    /**
     * 获得数据库中的数据
     */
    private List<LiuYanNumInfo> geLiuYanInfoList(String username) {
        mData = new ArrayList<LiuYanNumInfo>();
        SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("liuyan", null, "username="+username, null, null, null, null);
        if (cursor.moveToFirst()) {
            int num=0;
            do {
                num++;
            } while (cursor.moveToNext());
            mData.add(new LiuYanNumInfo("留言名称"+username,"留言次数"+num));
        }
        cursor.close();
        return mData;
    }
}

